<?php
/**
 * Created by PhpStorm.
 * User: lzhy@lzhy.me
 * Date: 2019/2/27 0027
 * Time: 17:38
 */
namespace Api\Event;

class ToolEvent
{
    /**
     * 微信md5签名
     * @param $data
     * @param string $key
     * @return string
     */
    public static function weChatMd5Sign($data,$key = '')
    {
        $data = array_filter($data);
        if(empty($data)){
            exit('请传入完整参数后再试');
        }
        ksort($data);
        $dataStr = urldecode(http_build_query($data));
        $formatStr = '%s';
        if($key){
            $formatStr = '%s&key=%s';
        }
        $sign = strtoupper(md5(sprintf($formatStr, $dataStr, $key)));
        return $sign;
    }

    /**
     * 数组转xml
     * @param $data
     * @return string
     */
    public static function arr2xml($data)
    {
        $xml = '<xml>';
        foreach ($data as $k => $v){
            $_format = '<%s><![CDATA[%s]]></%s>';
            if(is_numeric($v)){
                $_format = '<%s>%s</%s>';
            }
            if(is_array($v)){
                $v = json_encode($v);
            }
            $xml .= sprintf($_format,$k,$v,$k);
        }
        $xml .= '</xml>';
        return $xml;
    }

    /**
     * xml转数组
     * @param $xml
     * @return mixed
     */
    public static function xml2arr($xml)
    {
        libxml_disable_entity_loader(true);
        $xmlParseStr = simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA);
        $result = json_decode(json_encode($xmlParseStr),true);
        return $result;
    }

    /**
     * curl post请求
     * @param $url
     * @param $data
     * @return mixed
     */
    public static function curlPost($url,$data)
    {
        $header[] = 'Content-type: text/xml';
        $ch = curl_init ($url);
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch,CURLOPT_POST, 1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
        $response = curl_exec($ch);
        if(curl_errno($ch)){
            //
        }
        curl_close($ch);
        return $response;
    }

    /**
     * 生成随机字符串
     * @param int $len
     * @return string
     */
    public static function generatorRandStr($len = 32)
    {
        $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
        $shuffleStr = '';
        for ($i = 0; $i < $len; $i++){
            $shuffleStr .= $str[rand(0,strlen($str) - 1)];
        }
        return $shuffleStr;
    }

    /**
     * 统一下单默认参数
     * @param $appId
     * @param $mchId
     * @param $openid
     * @param $notifyUrl
     * @return array
     */
    public static function unifiedOrderDefaultParams($appId,$mchId,$openid,$notifyUrl)
    {
        $params = [
            'appid'  => $appId,
            'mch_id' => $mchId,
            'device_info' => '',
            'nonce_str' => self::generatorRandStr(),
            'sign' => '',
            'sign_type' => '',
            'body' => '',
            'detail' => '',
            'attach' => '',
            'out_trade_no' => '',
            'fee_type' => '',
            'total_fee' => '',
            'spbill_create_ip' => get_client_ip(),
            'time_start' => '',
            'time_expire' => '',
            'goods_tag' => '',
            'notify_url' => $notifyUrl,
            'trade_type' => 'JSAPI',
            'product_id' => '',
            'limit_pay' => '',
            'openid' => $openid,
            'receipt' => '',
            'scene_info' => '',
        ];

        return $params;
    }
}